package cn.intotw.rdcj.task.spiders.parser;

import cn.intotw.rdcj.task.bean.vo.BookListQueryVo;
import cn.intotw.rdcj.task.spiders.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: 笔趣阁解析类
 * @project: rdcj
 * @package: cn.intotw.rdcj.commodity.spiders
 * @date: 2020年07月24日 10时17分
 */
@Slf4j
public class BiqugeParser {
    public static List<BookListQueryVo> parseBookList(String htmlBody){
        ArrayList<BookListQueryVo> bookListQueryVos = new ArrayList<>();
        Document doc = Jsoup.parse(htmlBody);
        Elements select = doc.select("table.grid").select("tr");
        select.forEach(e->{
            Elements td = e.select("td");
            BookListQueryVo bookListQueryVo = new BookListQueryVo();
            if(td.size()==4) {
                bookListQueryVo.setBookName(td.get(0).text());
                bookListQueryVo.setTargetUrl(URLUtil.base64Encode(td.get(0).select("a").attr("href")));
                bookListQueryVo.setBookAuthor(td.get(2).text());
                bookListQueryVo.setLastSection(td.get(1).text());
                bookListQueryVo.setLastUpdateTime(td.get(3).text());
                bookListQueryVos.add(bookListQueryVo);
            }
        });
        return bookListQueryVos;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        parseBookList("<!doctype html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<title>新笔趣阁_书友最值得收藏的网络小说阅读网</title>\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" +
                "<meta name=\"keywords\" content=\"新笔趣阁,网络小说,小说阅读网,小说\" />\n" +
                "<meta name=\"description\" content=\"新笔趣阁是广大书友最值得收藏的网络小说阅读网，网站收录了当前最火热的网络小说，免费提供高质量的小说最新章节，是广大网络小说爱好者必备的小说阅读网。\" />\n" +
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"/images/biquge.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"http://libs.baidu.com/jquery/1.4.2/jquery.min.js\"></script>\n" +
                "<!--<script type=\"text/javascript\" src=\"http://cbjs.baidu.com/js/m.js\"></script>-->\n" +
                "<script type=\"text/javascript\" src=\"/images/bqg.js\"></script>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div id=\"wrapper\">\n" +
                "        <script>login();</script>\n" +
                "\n" +
                "        <div class=\"header\">\n" +
                "        \n" +
                "            <div class=\"header_logo\">\n" +
                "                <a href=\"http://www.xbiquge.la\" >新笔趣阁</a>\n" +
                "            </div>\n" +
                "            <script>bqg_panel();</script>\n" +
                "        \n" +
                "        </div>\n" +
                "        <div class=\"nav\">\n" +
                "            <ul>\n" +
                "                <li><a href=\"http://www.xbiquge.la/\">首页</a></li>\n" +
                "                <li><a href=\"/modules/article/bookcase.php\">我的书架</a></li>\n" +
                "                <li><a href=\"/xuanhuanxiaoshuo/\">玄幻小说</a></li>\n" +
                "                <li><a href=\"/xiuzhenxiaoshuo/\">修真小说</a></li>\n" +
                "                <li><a href=\"/dushixiaoshuo/\">都市小说</a></li>\n" +
                "                <li><a href=\"/chuanyuexiaoshuo/\">穿越小说</a></li>\n" +
                "                <li><a href=\"/wangyouxiaoshuo/\">网游小说</a></li>\n" +
                "                <li><a href=\"/kehuanxiaoshuo/\">科幻小说</a></li>\n" +
                "                <li><a href=\"/paihangbang/\">排行榜单</a></li>\n" +
                "                <li><a href=\"/xiaoshuodaquan/\">全部小说</a></li>\n" +
                "            </ul>\n" +
                "        </div>\n" +
                "<script type=\"text/javascript\">top_bar();</script>\n" +
                " \n" +
                "        <div id=\"main\">\n" +
                "        <div id=\"content\">\n" +
                "<style type=\"text/css\">\n" +
                "ul,li{margin:0;padding:0;}\n" +
                "#boxcase{margin:10px auto;width:975px;border:1px dashed #88C6E5;position:relative;}\n" +
                "#boxcase .title{top:-15px;width:100%;height:30px;line-height:30px;position:absolute;font-size:14px;color:#000;text-align:center;}\n" +
                "#boxcase .title span{background-color:#E9FAFF;}\n" +
                "#boxcase ul{padding:10px 20px 30px 20px;}\n" +
                "#boxcase ul li{float:left;width:25%;height:25px;line-height:25px;font-size:14px;color:#000;text-align:left; }\n" +
                "#boxcase ul li a{line-height:30px;font-size:14px;color:#FF0000; text-decoration:none;}\n" +
                "#boxcase ul li a:hover{color:#ff6600;}\n" +
                "</style>\n" +
                "<form action=\"\" method=\"post\" name=\"checkform\" id=\"checkform\" onsubmit=\"return check_confirm();\">\n" +
                "<div class=\"gridtop\">\n" +
                "搜索“帝霸\n" +
                "”结果\n" +
                "  </div>\n" +
                "<table class=\"grid\" width=\"100%\" align=\"center\">\n" +
                "  <tr align=\"center\">\n" +
                "    <th width=\"21%\">文章名称</th>\n" +
                "    <th width=\"30%\">最新章节</th>\n" +
                "    <th width=\"30%\">作者</th>\n" +
                "    <th width=\"7%\">更新</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/0/69/\" target=\"_blank\">帝霸</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/0/69/25707772.html\" target=\"_blank\">第4093章异象顿生</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">厌笔萧生</td>\n" +
                "    <td class=\"odd\" align=\"center\">07-27    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/30/30869/\" target=\"_blank\">三国之帝霸万界系统</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/30/30869/25719793.html\" target=\"_blank\">第一千一百六十章 无题</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">无谅</td>\n" +
                "    <td class=\"odd\" align=\"center\">07-27    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/39/39079/\" target=\"_blank\">星帝霸图</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/39/39079/18118852.html\" target=\"_blank\">新书：《我的星辰大海从黑科技开始》</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">乘雪</td>\n" +
                "    <td class=\"odd\" align=\"center\">03-19    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/46/46334/\" target=\"_blank\">网游之帝霸三国</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/46/46334/22237604.html\" target=\"_blank\">书已发布</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">孤剑行天下</td>\n" +
                "    <td class=\"odd\" align=\"center\">02-08    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/27/27507/\" target=\"_blank\">逆徒莫狂：邪帝霸宠异瞳妃</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/27/27507/15356040.html\" target=\"_blank\">第200章 与玉胚共存亡</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">玥临</td>\n" +
                "    <td class=\"odd\" align=\"center\">02-16    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/37/37629/\" target=\"_blank\">帝霸苍天</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/37/37629/17590319.html\" target=\"_blank\">第100章 撕烂嘴</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">杯酒煮闲愁</td>\n" +
                "    <td class=\"odd\" align=\"center\">05-23    </td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td class=\"even\"><a href=\"http://www.xbiquge.la/55/55399/\" target=\"_blank\">都市之帝霸归来</a></td>\n" +
                "    <td class=\"odd\"><a href=\"/55/55399/23712664.html\" target=\"_blank\">第70章 有些人你惹不起</a>\n" +
                "    \n" +
                "    </td>\n" +
                "    <td class=\"even\">节节响</td>\n" +
                "    <td class=\"odd\" align=\"center\">04-15    </td>\n" +
                "  </tr>\n" +
                "</table>\n" +
                "\n" +
                "</div>\n" +
                "        </div>\n" +
                "\n" +
                "        </div>\n" +
                "\n" +
                "        \n" +
                "        <div class=\"dahengfu\"><script type=\"text/javascript\">bottom();</script></div>\n" +
                "<div class=\"footer\">\n" +
                "            <div class=\"footer_link\"></div>\n" +
                "            <div class=\"footer_cont\">\n" +
                "                <script>footer();right();dl();</script>\n" +
                "            </div>\n" +
                "        </div>    \n" +
                "</body>\n" +
                "<script charset=\"utf-8\" src=\"http://www.baidu.com/js/opensug.js\"></script>\n" +
                "</html>\n");


    }
}
